package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.AgedmallDeductDetailMapper;
import org.linlinjava.litemall.db.dao.app.AppAgentMapper;
import org.linlinjava.litemall.db.domain.AgedmallDeductDetail;
import org.linlinjava.litemall.db.domain.AgedmallDeductDetailExample;
import org.linlinjava.litemall.db.domain.AgedmallGoodsDeductionConfExample;
import org.linlinjava.litemall.db.util.DeductionConfConstant;
import org.linlinjava.litemall.db.vo.app.DeductDetailListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class AgedmallDeductDetailService {
    @Resource
    private AgedmallDeductDetailMapper agedmallDeductDetailMapper;
    @Resource
    private AppAgentMapper appAgentMapper;

    public List<DeductDetailListVo> list(Integer userId, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);

        return appAgentMapper.deductDetailList(userId);
    }

    public void save(AgedmallDeductDetail agedmallDeductDetail) {
        agedmallDeductDetail.setAddTime(LocalDateTime.now());
        agedmallDeductDetail.setUpdateTime(LocalDateTime.now());
        agedmallDeductDetailMapper.insertSelective(agedmallDeductDetail);
    }

    public AgedmallDeductDetail queryBySubOrderAndGoodsId(String orderSn, Integer goodsId) {
        AgedmallDeductDetailExample example = new AgedmallDeductDetailExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(DeductionConfConstant.TYPE_GET)
                .andSubOrderSnEqualTo(orderSn)
                .andGoodsIdEqualTo(goodsId);
        return agedmallDeductDetailMapper.selectOneByExample(example);
    }

    public List<AgedmallDeductDetail> getListByOrderCode(List<String> childOrderCodeList) {
        if(childOrderCodeList == null || childOrderCodeList.size() == 0) {
            return new ArrayList<>();
        }
        AgedmallDeductDetailExample example = new AgedmallDeductDetailExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andSubOrderSnIn(childOrderCodeList)
                .andTypeEqualTo(DeductionConfConstant.TYPE_GET);
        return agedmallDeductDetailMapper.selectByExample(example);
    }
}
